#include "gamecontrol.h"

#include"gameobjectpool.h"

GameControl* GameControl::instance = nullptr;
GameControl::GameControl()
{
    this->setParent(Widget::widget);
}

void GameControl::GameInit()
{

    //对象池初始化
    GameObjectPool::Instance()->Init();
    GameObjectPool::Instance()->setParent(Widget::widget);


    //设置视图的父亲为窗口
    mGameView.setParent(Widget::widget);
    mGameView.show();

    LoadStartSence();//加载开始游戏场景

    //定时器初始化
    m_movetimer = new QTimer(Widget::widget); //人物移动定时器
    m_timer = new QTimer(Widget::widget);//人物回归静止状态定时器
    m_bulletmovetimer =new QTimer(Widget::widget); //子弹移动定时器
    m_createenemymonstertimer =new QTimer(Widget::widget);//怪物创建定时器




    //绑定定时器
    connect(m_timer, &QTimer::timeout, this,[=](){
        GameControl::slot_Result();
    });//一定时间不操作回归静止状态

    connect(m_movetimer, &QTimer::timeout, this,[=](){
        //人物行走
        GameControl::playermove(m_w,m_s,m_a,m_d);
        m_movetimer->stop();
        m_movetimer->start(GameDfine::m_movetime);

        //怪物行走
        for(auto monster : mEnemyMonsterList)
        {
            monster->MonsterMove(*player);
        }
        //血条移动
        xuetiao2.setPos(-356+player->blood*5,51);
        mRenwutouxiang.setPos(0,0);

        //好疼啊
        if(player->blood<=30&&haotenga==1)
        {
            haotengabgm->play();
            haotenga=0;
        }

        //死亡判定
        if(player->blood<=-5)
        {
            this->GameOver();
        }

    });
    //子弹移动
    connect(m_bulletmovetimer,&QTimer::timeout,this,[=](){
        for(auto bullet : mBulletList)
        {
            QPoint pzy;
            switch(bullet->B_zy)
            {
            case 1:
                pzy=QPoint(1,0);
                break;
            case 2:
                pzy=QPoint(-1,0);break;
            }

            bullet->BulletMove(pzy);

            //判断越界
            if(bullet->y()<-100||bullet->y()>1000||bullet->x()<-100||bullet->x()>1500)
            {
                //移除场景
                mSence.removeItem(bullet);
                //移除容器
                mBulletList.removeOne(bullet);
                //对象池回收对象
                GameObjectPool::Instance()->RecoveryGameObject(bullet);

            }
        }

        //碰撞检测
        Collision();

    });

    //怪物创建定时器绑定
    connect(m_createenemymonstertimer, &QTimer::timeout, this,[=](){
        GameControl::CrateEnemyMonster();
    });
}

void GameControl::LoadStartSence()
{
    mGameView.resize(1382,862);
    mGameView.setSceneRect(QRect(0,0,1380,860));//1380,860
    mStartSence.setSceneRect(QRect(0,0,1380,860));//开始游戏场景
    fightbiao.setPixmap(QPixmap(":/fightbiao.png"));
    mStartSence.addPixmap(QPixmap(":/beijing02.jpg"));
    mBackGround.setPixmap(QPixmap(":/beijing01.jpg"));
    mguizeSence.addPixmap(QPixmap(":/guize.png"));
    mRenwutouxiang.setPixmap(QPixmap(":/tou.png"));
    xuetiao1.setPixmap(QPixmap(":/xietiao1.png"));
    xuetiao2.setPixmap(QPixmap(":/xietiao2.png"));
    shibai.setPixmap(QPixmap(":/gameover.png"));

    mGameView.setScene(&mStartSence);

    mStartSence.addItem(&fightbiao);
    fightbiao.setPos(370,100);

    //音乐播放器初始化
    this->mOpenMusic= new QMediaPlayer(Widget::widget);
    this->mOpenMusic->setMedia(QUrl("qrc:/kaishibgm.mp3"));
    this->mFightMusic= new QMediaPlayer(Widget::widget);
    this->mFightMusic->setMedia(QUrl("qrc:/zhandoubgm.mp3"));
    this->mdaoxia= new QMediaPlayer(Widget::widget);
    this->mdaoxia->setMedia(QUrl("qrc:/daoxia.mp3"));
    this->haotengabgm= new QMediaPlayer(Widget::widget);
    this->haotengabgm->setMedia(QUrl("qrc:/haotenga.mp3"));
    this->mzoulubgm= new QMediaPlayer(this);   //人物行走bgm
    this->mzoulubgm->setMedia(QUrl("qrc:/zoulu.mp3"));

    //设置开始音乐
    this->mOpenMusic->play();
    this->mOpenMusic->setVolume(60);//调节音量大小
    auto starBtn = new QToolButton();
    starBtn->setStyleSheet("QToolButton {background-color: transparent;}");
    starBtn->setIcon(QIcon(":/Begin_text.png"));
    starBtn->setIconSize(QSize(200,50));
    starBtn->move(560,420);
    mStartSence.addWidget(starBtn);

    auto starBtn2 = new QToolButton();
    starBtn2->setStyleSheet("QToolButton {background-color: transparent;}");
    starBtn2->setIcon(QIcon(":/youxiguize.png"));
    starBtn2->setIconSize(QSize(200,50));
    starBtn2->move(560,500);
    mStartSence.addWidget(starBtn2);

    auto starBtn3 = new QToolButton();
    starBtn3->setStyleSheet("QToolButton {background-color: transparent;}");
    starBtn3->setIcon(QIcon(":/back.png"));
    starBtn3->setIconSize(QSize(200,50));
    starBtn3->move(1150,20);
    mguizeSence.addWidget(starBtn3);


    connect(starBtn,&QToolButton::clicked,this,[=](){//开始按钮
        this->LoadGameSence();//加载游戏场景
        this->GameStart();//开启定时器
    });

    connect(starBtn2,&QToolButton::clicked,this,[=](){//规则按钮
        mGameView.setScene(&mguizeSence);
        mGameView.show();

    });


    connect(starBtn3,&QToolButton::clicked,this,[=](){//返回按钮
        mGameView.setScene(&mStartSence);
        mGameView.show();

    });


    //随机数生成
    GameControl::sui=0;
    qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
    for(int i=0;i<1000;i++)
    {

        suiji[i] = qrand() % 5;                   //产生十以内的随机数(0-4)
    }

}

void GameControl::LoadGameSence()
{
    player =new Player();//创建一个人人物
    player->blood=100;
    mSence.setSceneRect(QRect(0,0,1380,860));//游戏场景1

    //图形元素添加到场景
    mSence.addItem(&mBackGround);

    //设置试图场景
    mSence.addWidget(player);

    mGameView.setScene(&mSence);
    mGameView.show();

    Widget::widget->grabKeyboard();//捕获键盘事件
    //开始战斗音乐
    this->mOpenMusic->stop();
    this->mFightMusic->play();
    this->mFightMusic->setVolume(30);//调节音量大小

    player->setStyleSheet("QLabel {background-color: transparent;}");//人物背景透明化
    player->move(610,550);//1000,550

    player->setMovie(player->m_movie3);
    player->show();

    //加载血条
    mSence.addItem(&xuetiao1);
    xuetiao1.setPos(144,45);
    mSence.addItem(&xuetiao2);
    xuetiao2.setPos(144,51);

    //加载人物头像
    mSence.addItem(&mRenwutouxiang);
    mRenwutouxiang.setPos(0,0);

}

void GameControl::GameStart()
{
    m_movetimer->start(GameDfine::m_movetime); //开启人物移动定时器
    m_bulletmovetimer->start(GameDfine::m_bulletmovetime);//开启子弹移动定时器
    m_createenemymonstertimer->start(GameDfine::m_createenemymonstertime);//开启怪物创建定时器

}

void GameControl::GameOver()
{
    //结束
    m_movetimer->stop();
    m_timer->stop();
    m_bulletmovetimer->stop();
    m_createenemymonstertimer->stop();
    mSence.addItem(&shibai);
    shibai.setPos(410,100);
    chengji.setText("您的分数为："+QString::number(EnemyMonster::DeadMonster));//EnemyMonster::DeadMonster
    chengji.setFont(QFont("华文琥珀",48));
    mSence.addItem(&chengji);
    chengji.setPos(400,600);
    mFightMusic->stop();
    this->mdaoxia->play();


}

void GameControl::slot_Result()
{
    m_timer->stop();
    player->setMovie(player->m_movie3);
    player-> m_movie3->start();
    player-> m_movie2->stop();
    player-> m_movie1->stop();
    this->mzoulubgm->pause();
}

//移动方向初始化
int GameControl::m_w=0;
int GameControl::m_s=0;
int GameControl::m_a=0;
int GameControl::m_d=0;

void GameControl::playermove(int m_w,int m_s,int m_a,int m_d)
{    int x = player->pos().x();
     int y = player->pos().y();
      if(m_w==1||m_a==1||m_s==1||m_d==1)
      {
          this->mzoulubgm->play();
      }
      //边界判断
      if(x<0)
      {
          m_a=0;
      }
      if(y<290)
      {
          m_w=0;
      }
      if(x>1265)
      {
          m_d=0;
      }
      if(y>720)
      {
          m_s=0;
      }
      //移动指令
      if(m_w==1&&m_a==0&&m_s==0&&m_d==0)//上
      {
          player->setMovie(player->m_movie4);
          player->m_movie4->start();
          player->m_movie3->stop();
          player->move(x,y-3*player->pSpeed);
      }
      else if(m_w==0&&m_a==1&&m_s==0&&m_d==0)//左
      {
          player->setMovie(player->m_movie1);
          if(player->m_movie4!=player->m_movie1)
          {
              player->m_movie4->stop();
          }
          player-> m_movie2->stop();
          player-> m_movie3->stop();
          player-> m_movie1->start();
          player->m_movie4=player->m_movie1;
          player->move(x-3*player->pSpeed,y);
      }
      else if(m_w==0&&m_a==0&&m_s==1&&m_d==0)//下
      {
          player->move(x,y+3*player->pSpeed);
          player->setMovie(player->m_movie4);
          player-> m_movie4->start();
          player->m_movie3->stop();
      }
      else if(m_w==0&&m_a==0&&m_s==0&&m_d==1)//右
      {
          player->setMovie(player->m_movie2);
          if(player->m_movie4!=player->m_movie2)
          {
              player->m_movie4->stop();
          }
          player->m_movie1->stop();
          player->m_movie3->stop();
          player->m_movie2->start();
          player-> m_movie4=player->m_movie2;
          player->move(x+3*player->pSpeed,y);
      }
      else if(m_w==1&&m_a==0&&m_s==0&&m_d==1)//上右
      {
          player->setMovie(player->m_movie2);
          if(player->m_movie4!=player->m_movie2)
          {
              player-> m_movie4->stop();
          }
          player-> m_movie1->stop();
          player-> m_movie3->stop();
          player-> m_movie2->start();
          player->m_movie4=player->m_movie2;
          player->move(x+2*player->pSpeed,y-2*player->pSpeed);
      }
      else if(m_w==1&&m_a==1&&m_s==0&&m_d==0)//上左
      {
          player->setMovie(player->m_movie1);
          if(player->m_movie4!=player->m_movie1)
          {
              player->m_movie4->stop();
          }
          player->m_movie2->stop();
          player->m_movie3->stop();
          player->m_movie1->start();
          player->m_movie4=player->m_movie1;
          player->move(x-2*player->pSpeed,y-2*player->pSpeed);
      }
      else if(m_w==0&&m_a==1&&m_s==1&&m_d==0)//下左
      {
          player->setMovie(player->m_movie1);
          if(player->m_movie4!=player->m_movie1)
          {
              player->m_movie4->stop();
          }
          player-> m_movie2->stop();
          player->m_movie3->stop();
          player->m_movie1->start();
          player->m_movie4=player->m_movie1;
          player->move(x-2*player->pSpeed,y+2*player->pSpeed);
      }
      else if(m_w==0&&m_a==0&&m_s==1&&m_d==1)//下右
      {
          player->setMovie(player->m_movie2);
          if(player->m_movie4!=player->m_movie2)
          {
              player-> m_movie4->stop();
          }
          player-> m_movie1->stop();
          player->m_movie3->stop();
          player-> m_movie2->start();
          player->m_movie4=player->m_movie2;
          player->move(x+2*player->pSpeed,y+2*player->pSpeed);
      }


}


void GameControl::PlayerBulletShoot(int zy)
{
    //对象池获取子弹
    GameObject* obj=GameObjectPool::Instance()->GetGameObject(GameObject::OT_BulletPlayer);
    Bullet* bullet = (Bullet*)obj;
    //创建子弹
    int zyx,zyy;
    switch(zy)
    {
    case 1://1右2左
        zyx=95;zyy=55;break;
    case 2:
        zyx=10;zyy=55;break;
    }
    QPoint pos(player->x()+zyx,player->y()+zyy);//子弹创建位置
    QPixmap bulletImg(":/zdz.png");
    //    Bullet* bullet =new Bullet(pos,bulletImg,zy);
    bullet->Init(pos,bulletImg,zy);
    mSence.addItem(bullet);//添加到场景
    //添加到子弹容器
    mBulletList.append(bullet);
}
void GameControl:: CreateMonster(QPoint mpi,int sp)
{

//    QTime randtime;
//    randtime = QTime::currentTime();
//    qsrand(randtime.msec()+randtime.second()*1000); //以当前时间ms为随机种子

//    qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));

//    int bex = qrand() % 5;     //产生十以内的随机数(0-4)

    int bex=GameControl::suiji[sui];
    GameControl::sui++;
    QPixmap pixmap;
    switch(bex)
    {
    case 0:pixmap = QPixmap(":/huo.png");break;
    case 1:pixmap = QPixmap(":/shui.png");break;
    case 2:pixmap = QPixmap(":/yan.png");break;
    case 3:pixmap = QPixmap(":/bing.png");break;
    case 4:pixmap = QPixmap(":/cao.png");break;
    }

    //对象池获取敌人
    GameObject* obj=GameObjectPool::Instance()->GetGameObject(GameObject::OT_EnemyMonster);
    EnemyMonster* monster=(EnemyMonster*)obj;
    //     EnemyMonster* monster = new EnemyMonster(mpi,pixmap);
    monster->Init(mpi,pixmap);
    monster->E_speed=sp;
    monster->blood=GameDfine::m_MonsrterBlood;
    mSence.addItem(monster);
    mEnemyMonsterList.append(monster);

}

void GameControl::Collision()//碰撞检测函数
{
    //遍历子弹
    for(int i=0;i<mBulletList.size();i++)
    {
        //遍历敌人
        for(int j=0;j<mEnemyMonsterList.size();j++)
        {
            if(mBulletList[i]->collidesWithItem(mEnemyMonsterList[j]))//碰撞检测
            {
                //子弹移除场景
                mSence.removeItem(mBulletList[i]);
                //播放爆炸声音
                mBulletList[i]->PlayBoomSound();
                //对象池回收子弹对象
                GameObjectPool::Instance()->RecoveryGameObject(mBulletList[i]);
                //移除管理器
                mBulletList.removeOne(mBulletList[i]);
                //扣除敌人血量
                mEnemyMonsterList[j]->blood-=1;
                if(player->blood<100)
                    player->blood++;//吸血

                if(mEnemyMonsterList[j]->blood<=0)
                {
                    mSence.removeItem(mEnemyMonsterList[j]);
                    GameObjectPool::Instance()->RecoveryGameObject(mEnemyMonsterList[j]);
                    mEnemyMonsterList.removeOne(mEnemyMonsterList[j]);
                    EnemyMonster::NowMonster--;
                    EnemyMonster::DeadMonster++;
                }

                break;

            }

        }
    }

}

void GameControl::CrateEnemyMonster()
{
    //怪物创建函数
    int a=3;int b=4;int c=5;
    if(EnemyMonster::DeadMonster>20)a=6;
    if(EnemyMonster::DeadMonster>25)b=8;
    if(EnemyMonster::DeadMonster>30)c=10;
    if(EnemyMonster::NowMonster==0&&EnemyMonster::DeadMonster<=9)
    {
        CreateMonster(QPoint(-200,400),5);
        CreateMonster(QPoint(1400,500),3);
        CreateMonster(QPoint(500,1000),4);
        EnemyMonster::NowMonster=3;
    }
    if(EnemyMonster::NowMonster==0&&EnemyMonster::DeadMonster>9)
    {
        CreateMonster(QPoint(-200,300),5);
        CreateMonster(QPoint(1400,300),3);
        CreateMonster(QPoint(300,1000),4);
        CreateMonster(QPoint(-200,500),a);
        CreateMonster(QPoint(1400,600),b);
        CreateMonster(QPoint(700,1000),c);
        EnemyMonster::NowMonster=6;
    }

}

int GameControl::suiji[];
int GameControl::sui=0;

